/**
 * 虚拟键盘
 * 
 * @class 虚拟键盘类
 * @extends zxt.ui.Interactor
 */
zxt.ui.Keyboard = function()
{
	zxt.ui.Interactor.call(this);
	this._target=null;
	this._kb = $('<div class="zxt-kbdiv"></div>');
};

zxt.defineClazz(zxt.ui.Keyboard, zxt.ui.Interactor);

/**
 * 返回一个随机数
 * 
 * @private
 * @function
 * @return {float} 浮点数
 */
zxt.ui.Keyboard.prototype._randomSort = function(){
	return Math.random() - 0.5;
};

/**
 * 返回一个随机符号数组
 * 
 * @private
 * @function
 * @return {int[]} 符号数组
 */
zxt.ui.Keyboard.prototype._getRandomSymbol = function()
{
	var symbolArray = [33,34,35,36,37,39,40,41,42,43,44,45,46,47,58,59,61,63,64,91,92,93,94,95,123,124,125,126];//生成一个符号数组
	symbolArray.sort(this._randomSort);
	for(var i=0;i<symbolArray.length;i++){
		symbolArray[i] = String.fromCharCode(symbolArray[i]);
	};
	symbolArray.push("<");
	symbolArray.push(">");
	return symbolArray;
};

/**
 * 返回一个随机数字数组
 * 
 * @private
 * @function
 * @return {int[]} 数字数组
 */
zxt.ui.Keyboard.prototype._getRandomNum = function()
{
	var numArray=new Array();
	for(var i=0;i<10;i++)
	  numArray[i]=i;//生成一个数组
	numArray.sort(this._randomSort);
	return numArray;
};

/**
 * 返回一个随机字母数组
 * 
 * @private
 * @function
 * @return {int[]} 字母数组
 */
zxt.ui.Keyboard.prototype._getRandomChar = function()
{
	var charArray=new Array();
	for(var i=0,j=97;j<123;i++,j++)
	  charArray[i]=j;//生成字母表
	charArray.sort(this._randomSort);
	//对字母进行翻译
	for(var i=0;i<charArray.length;i++){
		charArray[i] = String.fromCharCode(charArray[i]);
	};
	return charArray;
};

/**
 * <p>
 * 该控件把自己渲染成DOM节点的函数
 * </p>
 * 
 * @function
 * @return {DOM} 渲染成的DOM节点
 */
zxt.ui.Keyboard.prototype.renderSelf = function()
{
	var instance = this;
	switch(this._type){
		case 1:
			this._renderNum();
			break;
		case 3:
			this._renderAll();
			break;
		default:
			this._renderAll();
	};
	$("td",this._kb).mouseover(function() {
		this.className += " kbmouseover";
	}).mouseout(function() {
		this.className = this.className.replace(" kbmouseover","");
	}).click(function() {
		if(this.id == "kbclose") {
			instance.close();
			return false;
		};
		if(this.id == "kbcaps") {
			$.each($(".kbkey",this._kb),function(i,o) {
				var num = o.innerHTML.charCodeAt(0);
				if(num>96 && num<123)
					o.innerHTML = o.innerHTML.toUpperCase();
				else if(num>64 && num<91)
					o.innerHTML = o.innerHTML.toLowerCase();
			});
			return false;
		};
		if(this.id == "kbback") {
			instance.backspace();
			return false;
		};
		instance._onkeydown(this.innerHTML,this.id);	
	});
};

/**
 * <p>
 * 设置目标控件的值，当需要自定义对象控件设置方法时，用户可以重载该函数，同时重载getValue。
 * </p>
 * 
 * @function
 * @param {string} 需要设置的字符串
 */
zxt.ui.Keyboard.prototype.setValue = function(key){
	if(this._target.tagName=="INPUT"){
		this._target.value=key;
		return;
	}
	else{
		this._target.setValue(key);
		this._target.render();
		return;
	};
};

/**
 * <p>
 * 获得目标控件的值，当需要自定义对象控件取值方法时，用户可以重载该函数，同时重载setValue。
 * </p>
 * 
 * @function
 * @returns {string} 目标控件的值
 */
zxt.ui.Keyboard.prototype.getValue = function(){
	if(this._target.tagName=="INPUT"){
		return this._target.value;
	}
	else{
		return this._target.getValue();
	};
};

zxt.ui.Keyboard.prototype._onkeydown = function(key,keytype){
	this.onkeydown(key,keytype);
};

/**
 * <p>
 * 键盘按键处理函数，当用户需要对按下的按键经行特殊处理时可重载此函数
 * </p>
 * 
 * @function
 * @param {char} key 按键字符
 * @param {string} keytype 按键类型。"kbkey":字母按键，"kbnum":数字按键，"kbsym":符号按键
 */
zxt.ui.Keyboard.prototype.onkeydown = function(key,keytype){
	this.setValue(this.getValue()+key);
};

/**
 * <p>
 * 退格
 * </p>
 * 
 * @function
 */
zxt.ui.Keyboard.prototype.backspace = function(){
	if(this._target.tagName=="INPUT"){
		this._target.value=this._target.value.substring(0,this._target.value.length-1);
		return;
	}
	else{
		this._target.setValue((this._target.getValue()).substring(0,(this._target.getValue()).length-1));
		this._target.render();
		return;
	};
};

/**
 * <p>
 * 关闭键盘
 * </p>
 * 
 * @function
 */
zxt.ui.Keyboard.prototype.close = function(){
	this._kb.remove();
};

/**
 * <p>
 * 渲染全键盘
 * </p>
 * 
 * @private
 * @function
 */
zxt.ui.Keyboard.prototype._renderAll = function()
{
	var keyboard = '<table class="zxt-kbtable"><tr>';
	numArray = this._getRandomNum();
	charArray = this._getRandomChar();
	symbolArray = this._getRandomSymbol();
	//符号
	for(var i=0;i<22;i++)
	{
		if(i==12){
			keyboard += '<td id="kbback" colspan="2" class="zxt-kbcolspan">Backspace</td>';
			keyboard += "</tr><tr>";
		};
		keyboard += '<td class="zxt-kbsym">'+symbolArray[i]+'</td>';
	}
	//第一行数字
	for(var i=0;i<3;i++){
		keyboard += '<td class="zxt-kbnum">'+numArray[i]+'</td>';
	}
	keyboard += '<td class="zxt-kbsym">'+symbolArray[22]+'</td>';
	keyboard += "</tr><tr>";
	//字符
	for(var i=0;i<26;i++)
	{
		if (i%10==0 && i>0){
			//第二和第三行数字
			for(var j=0;j!=3;j++){
				keyboard += '<td class="zxt-kbnum">'+numArray[i*3/10+j]+'</td>';
			};
			keyboard += '<td class="zxt-kbsym">'+symbolArray[22+(i/10)]+'</td>';
			keyboard += "</tr><tr>";
		};
		
		keyboard += '<td class="zxt-kbkey">'+charArray[i]+'</td>';
	};
	
	keyboard += '<td id="kbcaps" colspan="2" class="zxt-kbcolspan">CapsLock</td>';
	keyboard += '<td id="kbclose" colspan="2" class="zxt-kbcolspan">Close</td>';
	//最后一行数字
	keyboard += '<td class="zxt-kbnum">'+numArray[9]+'</td>';
	keyboard += '<td class="zxt-kbsym">'+symbolArray[25]+'</td>';
	keyboard += '<td class="zxt-kbsym">'+symbolArray[26]+'</td>';
	keyboard += '<td class="zxt-kbsym">'+symbolArray[27]+'</td>';
	keyboard += '</tr></table>';
	this._kb.html(keyboard);
};

/**
 * <p>
 * 渲染数字键盘
 * </p>
 * 
 * @private
 * @function
 */
zxt.ui.Keyboard.prototype._renderNum = function()
{
	var keyboard = '<table class="zxt-kbtable_numonly"><tr>';
	numArray = this._getRandomNum();
	
	//数字
	for(var i=0;i<10;i++)
	{
		if (i%3==0 && i>0){
			
			keyboard += "</tr><tr>";
		};
		keyboard += '<td class="zxt-kbnum_only">'+numArray[i]+'</td>';
	}
	keyboard += '<td id="kbback" class="zxt-kbcolspan">Back</td>';
	keyboard += '<td id="kbclose" class="zxt-kbcolspan">Close</td>';
	keyboard += '</tr></table>';
	this._kb.html(keyboard);
};

/**
 * <p>
 * 显示键盘
 * </p>
 * 
 * @function
 * @param {DOM|zxt.ui.Widget} target 输入目标控件
 * @param {DOM} posDOM 显示位置DOM节点
 * @param {int} type 键盘类型。1代表数字键盘，3代表全键盘
 */
zxt.ui.Keyboard.prototype.show = function(target,posDOM,type)
{
	var all = 3;
	this._target=target;
	this._type = type||all;
	//this._target=target;
	this.render();
	zxt.util.dom.postionWithRel( this._kb.get(0), posDOM, false );
	this._kb.appendTo('body');
};